﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/1 18:14:56
 * ************************************************************/
namespace Mozlite.Common.Configuration
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Xml;
    using System.IO;

    /// <summary>
    /// 模板配置类。
    /// </summary>
    [Serializable]
    public class ThemeConfiguration
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Configuration.ThemeConfiguration"/>。
        /// </summary>
        public ThemeConfiguration() { }

        /// <summary>
        /// 模板唯一键。
        /// </summary>
        public Guid GuidKey { get; set; }
        /// <summary>
        /// 模板名称。
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 模板作者。
        /// </summary>
        public string Author { get; set; }
        /// <summary>
        /// 模板版本。
        /// </summary>
        public string Version { get; set; }
        /// <summary>
        /// 模板更新地址。
        /// </summary>
        public string UpgradeUrl { get; set; }
        /// <summary>
        /// 模板技术支持地址。
        /// </summary>
        public string SupportUrl { get; set; }
        /// <summary>
        /// 模板的英文名称，即文件夹路径。
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// LOGO图片。
        /// </summary>
        public string LogoUrl { get; set; }
        /// <summary>
        /// 描述。
        /// </summary>
        public string Summary { get; set; }
        /// <summary>
        /// 获取LOGO地址。
        /// </summary>
        /// <returns></returns>
        public string GetLogoUrl() {
            return Globals.GetUrl("Themes", this.Key, this.LogoUrl);
        }

        /// <summary>
        /// 加载所有模板。
        /// </summary>
        public static Dictionary<string, ThemeConfiguration> Themes {
            get {
                return Caches.Get<Dictionary<string, ThemeConfiguration>>("ThemeConfiguration::AllThemes", Globals.MinuteSeconds * 10, Globals.MinuteSeconds * 10, () => {
                    var themes = new Dictionary<string, ThemeConfiguration>(StringComparer.OrdinalIgnoreCase);
                    var root = new DirectoryInfo(Globals.MapPath("~/Themes"));
                    foreach (var directory in root.GetDirectories())
                    {
                        try
                        {
                            var theme = Globals.ConvertFileToObject<ThemeConfiguration>(Globals.Combine(directory.FullName, "Theme.config", true));
                            theme.Key = directory.Name;
                            themes.Add(directory.Name, theme);
                        }
                        catch (Exception e) {
                            Logs.Warn("模板加载失败：\r\n", "模板", EventID.Themes, e, "模板引擎");
                        }
                    }
                    return themes;
                });
            }
        }
    }
}